package com.td.erhome.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.td.erhome.R;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * Created by Administrator on 2017/4/18 0018.
 */

public class CheckAdapter extends RecyclerView.Adapter<CheckAdapter.HouseHolder> {

    private ArrayList<String> mList;
    private Context mContext;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private boolean mIsSelectable = false;

    public CheckAdapter(Context context,ArrayList<String> list) {
        mContext = context;
        mList = list;
        setSelectedFalse();
    }

    //全部设置为未选中

    public  void  setSelectedFalse(){
        for (int i = 0; i < mList.size(); i++) {
            mSelectedPositions.put(i, false);
        }
    }

    //获得选中条目的结果
    public HashMap<Integer,String> getSelectedItem() {
        HashMap<Integer,String> selectList = new HashMap<>();

        for (int i = 0; i < mList.size(); i++) {
            if (isItemChecked(i)) {
                selectList.put(i,mList.get(i));
            }
        }
        return selectList;
    }

    //设置给定位置条目的选择状态
    public void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    //根据位置判断条目是否可选
    private boolean isSelectable() {
        return mIsSelectable;
    }
    //设置给定位置条目的可选与否的状态
    private void setSelectable(boolean selectable) {
        mIsSelectable = selectable;
    }

    @Override
    public HouseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View  inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_check_item, null);
        HouseHolder cardHolder = new HouseHolder(inflate);
        return  cardHolder;
    }

    @Override
    public void onBindViewHolder(final HouseHolder holder, final int position) {
        //设置条目
          holder.mTvItem.setText(mList.get(position));
          holder.mTvItem.setBackgroundResource(R.drawable.shape_status_two);
          holder.mTvItem.setTextColor(mContext.getResources().getColor(R.color.color_444444));

        Boolean b = isItemChecked(position);
        if (!b) {
            setItemChecked(position, false);
            holder.mTvItem.setBackgroundResource(R.drawable.shape_status_two);
            holder.mTvItem.setTextColor(mContext.getResources().getColor(R.color.color_444444));
            Log.i("45545454","wei选中");
        } else if (b){
            setItemChecked(position, true);
            holder.mTvItem.setBackgroundResource(R.drawable.shape_status);
            holder.mTvItem.setTextColor(mContext.getResources().getColor(R.color.color_048d99));
            Log.i("45545454","选中");
        }

        //条目view的监听
          holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Boolean b = isItemChecked(position);
                if (b) {
                    setItemChecked(position, false);
                    holder.mTvItem.setBackgroundResource(R.drawable.shape_status_two);
                    holder.mTvItem.setTextColor(mContext.getResources().getColor(R.color.color_444444));
                    Log.i("45545454","wei选中");
                } else if (!b){
                    setItemChecked(position, true);
                    holder.mTvItem.setBackgroundResource(R.drawable.shape_status);
                    holder.mTvItem.setTextColor(mContext.getResources().getColor(R.color.color_048d99));
                    Log.i("45545454","选中");
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class HouseHolder extends RecyclerView.ViewHolder {

        private final TextView mTvItem;

        public HouseHolder(View itemView) {
            super(itemView);
            mTvItem = (TextView) itemView.findViewById(R.id.tv_item);
        }
    }
}
